﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmBordWidthManager : FrmMDIChildFormTemplate
    {
        BoardWidthController _controller = new BoardWidthController();
        BoardWidth _boardWidth = new BoardWidth();
        private static FrmBordWidthManager instance = null;
        private  FrmBordWidthManager()
        {
            InitializeComponent();
        }

        public static FrmBordWidthManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmBordWidthManager();
                }
                return instance;
            }
        }

        private void FrmBordWidthManager_Load(object sender, EventArgs e)
        {
            _controller.Grid = gridControlBordWidth;
            _controller.View = gridViewBordWidth;
            _controller.GetData();
        }

        private void gridViewBordWidth_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _boardWidth = (BoardWidth)gridViewBordWidth.GetFocusedRow();
            if (_boardWidth != null)
            {
                txtWidth.Text = _boardWidth.Width.ToString();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtWidth.Focus();
                return;
            }
            if (CheckData())
            {
                BoardWidth boardWidth = new BoardWidth();
                boardWidth.Width = Convert.ToDouble(txtWidth.Text);
                _controller.Add(boardWidth);

                //link datagrid
                _controller.Grid = gridControlBordWidth;
                _controller.View = gridViewBordWidth;
                _controller.GetData();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtWidth.Focus();
                return;
            }
            if (CheckData())
            {
                BoardWidth boardWidth = _controller.GetBoardWidthObj(_boardWidth.BoardWidthID);
                boardWidth.Width =Convert.ToDouble( txtWidth.Text);
                _controller.Update();


                //refresh datagrid
                _controller.Grid = gridControlBordWidth;
                _controller.View = gridViewBordWidth;
                _controller.GetData();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                BoardWidth boardWidth = _controller.GetBoardWidthObj(_boardWidth.BoardWidthID);
                _controller.Delete(boardWidth);

                //refresh datagrid
                _controller.Grid = gridControlBordWidth;
                _controller.View = gridViewBordWidth;
                _controller.GetData();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtWidth.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtWidth.Text == string.Empty)
            {
                txtWidth.Focus();
                MessageBox.Show("纸型宽度不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtWidth.Enabled = bl;

            txtWidth.Text = string.Empty;

            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtWidth.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmBordWidthManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void txtWidth_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Convert.ToDouble(txtWidth.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWidth.Text = string.Empty;
            }
        }


    }
}
